import Vue from 'vue'
import Router from 'vue-router'
import request from '@/utils/request'
import pingtu from '@/view/pingtu/selectMain.vue'
import wuziq from '@/view/wuziq/wuzqmain.vue'
import zhifu from '@/components/HelloWorld.vue'
import websocket from '@/components/websocket/websocket.vue'
import mainchat from '@/view/chat/mainchat.vue'

import {Message,MessageBox} from 'element-ui'; 
Vue.use(Router)


let router = new Router({
  routes: [
    {
      path: '/pingtu',
      name: 'pingtu',
      component: pingtu
    },
    {
      path: '/zhifu',
      name: 'zhifu',
      component: zhifu
    },{
      path: '/wuziq',
      name: 'wuziq',
      component: wuziq
    },{
      path: '/websocket',
      name: 'websocket',
      component: websocket
    },{
      path: '/mainchat',
      name: 'mainchat',
      component: mainchat
    }
  ] 
})
export default router
router.beforeEach((to, from, next) => {
  console.log(to)
  if(to.path=='/zhifu'){
    next();
  }else if(to.query.name){
    next();
  }else{
    MessageBox.prompt('请输入姓名', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(({ value }) => {


      let data=request({
        url: '/pingtu/exist-user',
        method: 'post',
        data:{"name":value}
      })
      Promise.all([data]).
      then(res => {
        let data=res[0];
        if(data.code==200){
          next({
            path:to.path,
            query:{"name":value}
          })
        }else{
          Message({
            type: 'error',
            message: "你输入的暂时没有账号！联系一下管理员"
          });
          router.push("/zhifu");
        }
      });
    }).catch(() => { 
    });
  }
 
   
})